﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_Scholarship : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        InitialData_BLL iniData = new InitialData_BLL();
        IMasEducationDegreeBLL degreeBLL = new MasEducationDegreeBLL();
        ITrainingScholarshipTypeBLL SchTypeBLL = new TrainingScholarshipTypeBLL();
        ITrainingScholarshipFundBLL SchFundBLL = new TrainingScholarshipFundBLL();
        TrainingScholarshipBLL SchBLL = new TrainingScholarshipBLL();
        ComboBLL combo = new ComboBLL();
        InitialData_BLL iniDataCur = new InitialData_BLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
                LoadScholaType();
                LoadScholarFund();
                LoadCountry();
                //DDLBgmSource();
                LoadEduDegrees();
            }
        }

        private void LoadDataGrid()
        {

            DataSet ListDataCommand = SchBLL.GetData("");
            DataTable table = ListDataCommand.Tables[0];            
            RadGrid1.DataSource = ListDataCommand.Tables[0];
            RadGrid1.DataBind();
            

        }
        //protected void DDLBgmSource()
        //{

        //    ddlBgmSource.DataSource = iniDataCur.ListBgmSource("");
        //    ddlBgmSource.DataBind();
        //}
        protected void LoadEduDegrees()
        {
            IList<MasEducationDegreeInfo> Info = degreeBLL.ListEducationDegree();
            cbEduDegrees.DataSource = Info;
            cbEduDegrees.DataBind();
        }

        protected void LoadScholaType()
        {
            cbScholaType.DataSource = SchTypeBLL.GetFull();
            cbScholaType.DataBind();
        }
        private void LoadCountry()
        {
            DataTable InfoList = combo.ListCountries();
            ddlCountry.DataSource = InfoList;
            ddlCountry.DataBind();
        }
        protected void LoadScholarFund()
        {
            ddlScholarFund1.DataSource = SchFundBLL.GetFull();
            ddlScholarFund1.DataBind();
            ddlScholarFund2.DataSource = SchFundBLL.GetFull();
            ddlScholarFund2.DataBind();
        }
       
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            string a = e.CommandName;
            if (e.CommandName == "AddData")
            {
                UpdateUI("AddNew");
            }
            else if (e.CommandName == "DataEdit")
            {
                string strScholarSeq = RadGrid1.Items[e.Item.ItemIndex]["scholar_seq"].Text;
                txtscholarSeq.Text = strScholarSeq;
                DataSet ListDataCommand = SchBLL.GetData(strScholarSeq);
                DataTable table = ListDataCommand.Tables[0];
                if (table.Rows.Count > 0)
                { 
                    cbScholaType.SelectedValue = table.Rows[0]["scholar_type_seq"].ToString(); 
                    ddlScholarFund1.SelectedValue = table.Rows[0]["scholar_fund_seq"].ToString();
                    if (table.Rows[0]["scholar_fund_seq2"].ToString().Length > 0)
                    { ddlScholarFund2.SelectedValue = table.Rows[0]["scholar_fund_seq2"].ToString(); }
                    cbEduDegrees.SelectedValue = table.Rows[0]["edu_degree_seq"].ToString(); 
                    ddlCountry.SelectedValue = table.Rows[0]["country_seq"].ToString(); 
                    //txtEmpPreCon.Text = table.Rows[0][""].ToString();
                    txtBudget.Text = table.Rows[0]["amount"].ToString();
                    string strStatus = table.Rows[0]["closed"].ToString();
                    if (strStatus == "1")
                    { rbStatus.Checked = true; }
                    else
                    { rbStatus.Checked = false; }
                    txtDateSignStart.SetValue(string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_sign_start"]));
                    txtDateSignEnd.SetValue(string.Format("{0:dd/MM/yyyy}",table.Rows[0]["date_sign_end"]));
                    //txtDateSignEnd
                    txtYearEnd.Text = table.Rows[0]["edu_year_end"].ToString();
                    txtYearStart.Text = table.Rows[0]["edu_year_start"].ToString();
                }
                UpdateUI("Update");
            }            
            else if (e.CommandName == "Delete")
            {
                string seq = RadGrid1.Items[e.Item.ItemIndex]["scholar_seq"].Text;
                SchBLL.Delete(seq, ssMng.EmpSeq);
            }
            
            LoadDataGrid();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int bStatus = 0;
            if (rbStatus.Checked == true)
            { bStatus = 1; }
            else
            { bStatus = 0; }
            
                SchBLL.Update(txtscholarSeq.Text, "", cbScholaType.SelectedValue, ddlScholarFund1.SelectedValue, ddlScholarFund2.SelectedValue, cbEduDegrees.SelectedValue
                            , ddlCountry.SelectedValue, "", txtBudget.Text, bStatus, txtDateSignStart.GetDateValue(), txtDateSignEnd.GetDateValue()
                            , txtYearEnd.Text, txtYearStart.Text, ssMng.EmpSeq);
            
            
            int UserID = ssMng.EmpSeqI;
            UpdateUI("Save");            
            LoadDataGrid();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int bStatus = 0;
            if (rbStatus.Checked == true)
                { bStatus = 1; }
                else
                { bStatus = 0; }
           
            
                SchBLL.Insert("", cbScholaType.SelectedValue, ddlScholarFund1.SelectedValue, ddlScholarFund2.SelectedValue, cbEduDegrees.SelectedValue
                            , ddlCountry.SelectedValue, "", txtBudget.Text, bStatus, txtDateSignStart.GetDateValue(), txtDateSignEnd.GetDateValue()
                            , txtYearEnd.Text, txtYearStart.Text, ssMng.EmpSeq);
            
            UpdateUI("Save");
            LoadDataGrid();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("Cancel");
            ClearControl();
        }
        protected void ClearControl()
        {
            //txtDateSignStart.SetValue("");
            //txtDateSignEnd.SetValue("");
            txtYearEnd.Text = "";
            txtYearStart.Text = "";
            //    rblCoutry.SelectedValue = "1";
            //    lblError.Text = "";
            //    txtScholaType.Text = "";
            //    txtScholaTypeSeq.Text = "";
        }
        protected bool checkValidate()
        {
            bool bOK = true;
            string strScholaType = cbScholaType.Text;
            if (strScholaType.Length == 0)
            {
                bOK = false;
                lblError.Text = "* กรุณาเลือกแหล่งทุน";
            }
            string strScholarFund1 = ddlScholarFund1.Text;
            if (strScholarFund1.Length == 0)
            {
                bOK = false;
                lblError.Text = "* กรุณาเลือกแหล่งเงิน";
            }
            string DateSignStart = txtDateSignStart.GetStringValue();
            if (DateSignStart.Length == 0)
            {
                bOK = false;
                lblError.Text = "* กรุณากรอกปีการศึกษาที่เริ่มให้ทุน";
            }
            string DateSignEnd = txtDateSignEnd.GetStringValue();
            if (DateSignEnd.Length == 0)
            {
                bOK = false;
                lblError.Text = "* กรุณากรอกปีการศึกษาที่สิ้นสุดการให้ทุน";
            }
            if (txtYearStart.Text.Length == 0)
            {
                bOK = false;
                lblError.Text = "* กรุณาเลือกวันที่ปิดรับสมัคร";
            }
            if (DateSignEnd.Length == 0)
            {
                bOK = false;
                lblError.Text = "* กรุณาเลือกวันที่ปิดรับสมัคร";
            }
            return bOK;
        }
        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    //btnUpdate.Visible = false;
                    //btnSave.Visible = true;

                    break;

                case "Cancel":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "Update":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnSave.Visible = false;
                    btnUpdate.Visible = true;

                    break;
            }
        }
    }
}

